/**
 * 
 */
package Community.fb;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.EnumSet;
import java.util.List;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpState;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.params.HttpClientParams;
import org.w3c.dom.Document;

import com.google.code.facebookapi.FacebookException;
import com.google.code.facebookapi.FacebookJaxbRestClient;
import com.google.code.facebookapi.FacebookXmlRestClient;
import com.google.code.facebookapi.ProfileField;
import com.google.code.facebookapi.schema.Album;
import com.google.code.facebookapi.schema.FriendsGetResponse;
import com.google.code.facebookapi.schema.Photo;
import com.google.code.facebookapi.schema.PhotosGetAlbumsResponse;
import com.google.code.facebookapi.schema.PhotosGetResponse;
import com.google.code.facebookapi.schema.User;
import com.google.code.facebookapi.schema.UsersGetInfoResponse;

/**
 * @author Hardeep
 *
 */
public class FacebookUser {
	private FacebookJaxbRestClient jaxClient;
	private FacebookXmlRestClient xmlClient;
	private String session;
	private long userID = -1;
	public static EnumSet<ProfileField> fields = EnumSet.allOf(ProfileField.class);
	
	//http://wiki.developers.facebook.com/index.php/Login_Desktop_App
	public FacebookUser(String username, String password) 
	throws HttpException, IOException, FacebookException {
		System.out.println("Trying to log in...");
		
		jaxClient = new FacebookJaxbRestClient(FBProperties.APIKEY, 
				FBProperties.SECRET);
		
		System.out.println("Creating token...");
		String token = jaxClient.auth_createToken();

		HttpClient http = new HttpClient();
		http.setParams(new HttpClientParams());
		http.setState(new HttpState());

		System.out.println("Connecting to " + FBProperties.LOGIN_URL);
		GetMethod get = new GetMethod(FBProperties.LOGIN_URL + "?api_key=" + 
				FBProperties.APIKEY + "&v=1.0&auth_token=" + token);

		http.executeMethod(get);

		System.out.println("Posting login info...");
		PostMethod post = new PostMethod(FBProperties.LOGIN_URL);
		post.addParameter(new NameValuePair("api_key", FBProperties.APIKEY));
		post.addParameter(new NameValuePair("v", "1.0"));
		post.addParameter(new NameValuePair("auth_token", token));
		post.addParameter(new NameValuePair("email", username));
		post.addParameter(new NameValuePair("pass", password));
		http.executeMethod(post);

		System.out.println("Getting session info...");
		session = jaxClient.auth_getSession(token);
		xmlClient= new FacebookXmlRestClient(FBProperties.APIKEY, 
				FBProperties.SECRET, session);
		
		System.out.println("User logged in, SSID: " + session);
		userID = jaxClient.users_getLoggedInUser();
		System.out.println("UserID: " + userID);
	}
	
	public void getFriends() throws FacebookException {
		System.out.println("Getting friends...");
		FriendsGetResponse friendsResp = (FriendsGetResponse)jaxClient.friends_get();
		System.out.println("Getting info...");
		jaxClient.users_getInfo(friendsResp.getUid(), fields);
		System.out.println("Getting response...");
		UsersGetInfoResponse uresp =(UsersGetInfoResponse)jaxClient.getResponsePOJO();
		
		List<User> friends = uresp.getUser();
		for ( User friend : friends ) {
			System.out.println("---" + friend.getFirstName() + " " + friend.getLastName() + "---");
			System.out.println("Pic: " + friend.getPic());
			System.out.println("Sex: " + friend.getSex());
			System.out.println("Bday: " + friend.getBirthday());
		}
	}
	
	public List<Album> getAlbums() throws FacebookException, IOException {
		PhotosGetAlbumsResponse alb = (PhotosGetAlbumsResponse)jaxClient.photos_getAlbums(userID);
		return alb.getAlbum();
	}

	public List<Photo> getPhotos(Album a) throws FacebookException, IOException {
		PhotosGetResponse photos = (PhotosGetResponse)jaxClient.photos_getByAlbum(a.getAid());
		return photos.getPhoto();
	}
}
